/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.ctrl.action;

import de.uhilger.servicehub.ServiceHub;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;

/**
 * Abstract base class for actions of application ServiceHub
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public abstract class AbstractServiceHubAction extends AbstractAction {

  /** the ServiceHub that owns this action */
  protected ServiceHub owner;
  /** listeners to results of this action */
  protected List listeners = new ArrayList();

  /**
   * Create a new object of class AbstratServiceHubAction
   * @param owner  the owner of this action
   */
  public AbstractServiceHubAction(ServiceHub owner) {
    super();
    this.owner = owner;
  }

  /**
   * Add an ActionResultListener
   * @param listener  the listener to add
   */
  public void addActionResultListener(ActionResultListener listener) {
    listeners.add(listener);
  }

  /**
   * Remove an ActionResultListener
   * @param listener the listener to remove
   */
  public void removeActionResultListener(ActionResultListener listener) {
    listeners.remove(listener);
  }

  /**
   * Notify listeners of the result of this action
   * @param event   the event that led to this result
   * @param result  the result to notify listeners of
   */
  protected void fireActionResult(ActionEvent event, Object result) {
    Iterator i = listeners.iterator();
    while(i.hasNext()) {
      Object o = i.next();
      if(o != null && o instanceof ActionResultListener) {
        ((ActionResultListener) o).resultPresent(event, result);
      }
    }
  }

}
